import sys

sys.setrecursionlimit(1 << 25)  # 设置更大的递归深度

n = int(input())
score = [0] + list(map(int, input().split()))
edges = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    edges[u].append(v)
    edges[v].append(u)
visited = set()
ans = 0


def postOrder(node):
    global ans
    if node in visited: return 0
    visited.add(node)
    val = score[node]
    for nn in edges[node]:
        if nn in visited: continue
        s = postOrder(nn)
        if s > 0:
            val += s
    ans = max(ans, val)
    return val


postOrder(1)
print(ans)
